/**数据查询组件**/
Vue.component('query', {
    props: ['value'],
    template:'#query-page-template',
    data:function () {
        return {
            attrs:{
                shengxiao:'生肖',
                wuxing:'五行肖',
                jixiong: '吉凶肖',
                jiaye:'家野肖',
                tiandi:'天地肖',
                yinyang:'阴阳肖',
                danshuang:'单双笔肖',
                heibai:'黑白肖',
                nannv:'男女肖',
                yanse:'三色肖',
                jijie:'四季肖',
                fangxiang:'方位肖',
                qqsh:'琴棋书画肖',
                bo:'三色波号',
                daxiao:'大小数',
                he:'合单双',
            },
            currAttr:'shengxiao',
            pos:['正码一', '正码二', '正码三', '正码四', '正码五', '正码六', '特码'],
            currPos:[],
            tab:'query',
            list:[],
            page:{page:1, limit:100, count:10000}
        }
    },
    created:function () {
        this.getList(false);
    },
    methods:{
        setCurrAttr:function (attr) {
            this.currAttr = attr;
            let lists = this.list, _self = this;

            this.list = [];
            setTimeout(function () {
                _self.list = lists;
            }, 100);
        },
        changeTab:function (index) {
            this.$emit('changeroute', index)
        },
        getList:function () {
            let conditions = {};
            let _self = this, offset = (this.page.page - 1) * this.page.limit;
            database.getAll('code', conditions, 'qi desc', offset, this.page.limit, function (res, err) {
                _self.list = !err ? res : [];
            });
            database.getCount('code', conditions, function (res, err) {
                !err && (_self.page.count = res);
            });
        },
        jump:function (page) {
            this.page.page = page;
            this.getList();
        },
        isShowPos:function (index) {
            return this.currPos.length < 1 || (this.currPos.indexOf(index) > -1)
        },
        resetCurrPos:function () {
            this.currPos = [];
        },
        edit:function (time) {
            this.$emit('changeroute', 'add', time);
        }
    }
});



/**码单元格**/
Vue.component('ma', {
    data:function() {
        return {item:{code:0, attr:''}}
    },
    props: ['num', 'attr'],
    template:'#ma-template',
    methods:{
        formatCode:function (code) {
            code += '';
            return code.length < 2 ? '0' + code : code;
        },
        isColor:function (color) {
            let config = zodiac.config.boZodiac;
            for (let index in  config) {
                if (config[index].indexOf(this.num) > -1) return color === index;
            }
            return false;
        }
    },
    created:function () {
        let item = zodiac.getCodeZodiac(this.num);
        this.item.code = this.formatCode(item.code);
        this.item.attr = this.attr === 'shengxiao' ? item.name : item[this.attr];
    }
});


/**分页组件**/
Vue.component('pager', {
    props: ['currPage', 'count', 'limit'],
    template:'#pager-template',
    computed:{
        pages:function () {
            let sp = this.currPage - 4, ep = this.currPage + 5, tp = Math.ceil(this.count/this.limit), pages = [];
            if (tp < 2) {
                return [];
            }

            let eoff = ep - tp;//tp:totalpage

            if(eoff>0){
                sp = sp - eoff;
            }
            if(sp<=0){
                ep = ep -sp + 1;
            }

            if(this.currPage > 1) {
                pages.push({name:'上一页', num:this.currPage-1, type:'pre'});
            }

            for(let i= sp; i<=ep; i++){
                if(i > 0 && i <= tp){
                    pages.push({name:i, num:i,type:''});
                }
            }

            if(this.currPage < tp) {
                pages.push({name:'下一页', num:this.currPage + 1, type:'next'});
            }
            return pages;
        }
    },
    methods:{
        jump:function (page) {
            this.$emit('jump', page)
        }
    }
});